﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace SilverlightSdms.Web.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PaychecksService" in code, svc and config file together.
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PaychecksService
    {
        private readonly SdmsEntities _context = new SdmsEntities();

        [OperationContract]
        public bool AddPaycheck(int userID, int month, double hoursCompleted)
        {
            var foundUser = _context.Users.First(u => u.Id == userID);
            bool isPaycheckAlreadyExist = isPaycheckExist(foundUser, month);

            if (!isPaycheckAlreadyExist)
            {
                int hourlySalary = foundUser.HourlySalary;

                Paycheck newPaycheck = new Paycheck();

                newPaycheck.ForMonth = month;
                newPaycheck.Hours = (int)hoursCompleted;
                newPaycheck.HourlySalary = hourlySalary;
                newPaycheck.User = foundUser;
                newPaycheck.Fine = newPaycheck.Bonus = 0;
                newPaycheck.IsCollected = false;

                _context.Paychecks.AddObject(newPaycheck);
                _context.SaveChanges();
            }

            return isPaycheckAlreadyExist;
        }

        [OperationContract]
        public bool AddBonus(int userID, int month, int bonusAmount)
        {
            var foundUser = _context.Users.First(u => u.Id == userID);
            Paycheck paycheckToUpdate = getPaycheck(foundUser, month);
            bool isPaycheckFound = paycheckToUpdate != null;

            if (isPaycheckFound)
            {
                _context.Paychecks.Attach(paycheckToUpdate);
                paycheckToUpdate.Bonus += bonusAmount;
                
                _context.Paychecks.ApplyCurrentValues(paycheckToUpdate);
                _context.AcceptAllChanges();
                _context.ObjectStateManager.ChangeObjectState(paycheckToUpdate, System.Data.EntityState.Modified);
                _context.SaveChanges();
            }

            return isPaycheckFound;
        }

        [OperationContract]
        public bool FineSalary(int userID, int month, int fineAmount)
        {
            var foundUser = _context.Users.First(u => u.Id == userID);
            Paycheck paycheckToUpdate = getPaycheck(foundUser, month);
            bool isPaycheckFound = paycheckToUpdate != null;

            if (isPaycheckFound)
            {
                _context.Paychecks.Attach(paycheckToUpdate);
                paycheckToUpdate.Fine += fineAmount;

                _context.Paychecks.ApplyCurrentValues(paycheckToUpdate);
                _context.AcceptAllChanges();
                _context.ObjectStateManager.ChangeObjectState(paycheckToUpdate, System.Data.EntityState.Modified);
                _context.SaveChanges();
            }

            return isPaycheckFound;
        }

        private Paycheck getPaycheck(User user, int month)
        {
            foreach (Paycheck paycheck in user.Paychecks)
            {
                if (paycheck.ForMonth == month)
                {
                    return paycheck;
                }
            }
            return null;
        }

        private bool isPaycheckExist(User user, int month)
        {
            return getPaycheck(user, month) != null;
        }
    }
}
